package com.aaa.common.utils;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * ToDo
 *
 * @author hexinxiang
 * @date 2024/8/20 14:22
 */
public class RedisJsonUtils {
    private static Logger logger = LoggerFactory.getLogger(com.aaa.common.utils.RedisJsonUtils.class);
    private static ObjectMapper objectMapper;
    private static JavaTimeModule javaTimeModule;
    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    static {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
            javaTimeModule = new JavaTimeModule();

            objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
            objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            objectMapper.setDateFormat(new SimpleDateFormat(DATE_FORMAT));
            objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.EVERYTHING, JsonTypeInfo.As.PROPERTY);

            javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
            javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));

            objectMapper.registerModule(javaTimeModule);
        }
    }

    public static void setObjectMapper(ObjectMapper objectMapper){
        com.aaa.common.utils.RedisJsonUtils.objectMapper = objectMapper;
    }

    public static ObjectMapper getObjectMapper(){
        return com.aaa.common.utils.RedisJsonUtils.objectMapper;
    }

    public static JavaTimeModule getJavaTimeModule() { return javaTimeModule; }

    /**
     * 对象转Json格式字符串
     * @param obj 对象
     * @return Json格式字符串
     */
    public static <T> String toJson(T obj) {
        if (obj == null) {
            return null;
        }
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 对象转Json格式字符串(格式化的Json字符串)
     * @param obj 对象
     * @return 美化的Json格式字符串
     */
    public static <T> String toJsonPretty(T obj) {
        if (obj == null) {
            return null;
        }
        try {
            return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 字符串转换为自定义对象
     * @param jsonStr 要转换的字符串
     * @return 自定义对象
     */
    public static <T> T fromJson(String jsonStr){
        if(StringUtils.isEmpty(jsonStr)){
            return null;
        }
        try {
            return (T) objectMapper.readValue(jsonStr, Object.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
